<!-- eslint-disable vue/multi-word-component-names -->
<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/rear-end/index' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>商品管理</el-breadcrumb-item>
  <el-breadcrumb-item>参数列表</el-breadcrumb-item>
</el-breadcrumb>
<!-- 卡片视图区域 -->
<el-card>
    <el-alert title="注意：只允许为第二级分类设置相关参数！" type="warning" :closable="false" show-icon>
  </el-alert>
  <el-row class="cat_opt"><el-col>
    <span>选择商品分类：</span>
    <!-- 级联选择框 -->
    <el-cascader
    expand-trigger="hover"
    v-model="selectedCateKeys"
    :options="catelist"
    :props="cateProps"
    @change="handleChange"></el-cascader>
  </el-col></el-row>
  <!-- tab页签区域 -->
  <el-tabs v-model="activeName" @tab-click="handleTabClick">
    <!-- 添加动态参数的面板 -->
    <el-tab-pane label="动态参数" name="many">
        <el-button type="primary" size="mini" :disabled="isBtnDisabled" @click="addDialogVisible=true">添加参数</el-button>
    <!-- 动态参数表格 -->
    <el-table :data="manyTableData" border stripe>
        <!-- 展开行 -->
<el-table-column type="expand">
    <template slot-scope="scope">
        <el-tag v-for="(item,i) in scope.row.attrVals" 
        :key="i" closable @close="handleClose(i,scope.row)">
        {{item}} 
       </el-tag>
       
        </template>
</el-table-column>
<el-table-column type="index"></el-table-column>
<el-table-column label="参数名称" prop="attrName"></el-table-column>
<el-table-column label="操作" >
    <template slot-scope="scope">
        <el-button type="primary" @click="showEditDialog(scope.row)" icon="el-icon-edit">编辑</el-button>
        <el-button type="danger" @click="delParams(scope.row)" icon="el-icon-delete" >删除</el-button>
    </template>
</el-table-column>
</el-table>
    </el-tab-pane>
   <!-- 添加静态属性的面板 -->
    <el-tab-pane label="静态属性" name="only">
        <el-button type="primary" size="mini" :disabled="isBtnDisabled" @click="addDialogVisible=true">添加属性</el-button>
    <!-- 静态属性表格 -->
    <el-table :data="onlyTableData" border stripe>
        <!-- 展开行 -->
        <el-table-column type="expand">
    <template slot-scope="scope">
        <el-tag v-for="(item,i) in scope.row.attrVals" :key="i" closable @close="handleClose(i,scope.row)">
        {{item}}
    </el-tag>
        <!-- 输入文本框 -->
        <el-input
        class="input-new-tag"
        v-if="scope.row.inputVisible"
        v-model="scope.row.inputValue"
        ref="saveTagInput"
        size="small"
        @keyup.enter.native="handleInputConfirm(scope.row)"
        @blur="handleInputConfirm(scope.row)"
        >
        </el-input>
<!-- 添加按钮 -->
        <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">+ New Tag</el-button>
        </template>
        </el-table-column>
<el-table-column type="index"></el-table-column>
<el-table-column label="属性名称" prop="attrName"></el-table-column>
<el-table-column label="操作" >
    <template slot-scope="scope">
        <el-button type="primary" @click="showEditDialog(scope.row)" icon="el-icon-edit">编辑</el-button>
        <el-button type="danger" @click="delParams(scope.row)" icon="el-icon-delete" >删除</el-button>
    </template>
</el-table-column>
</el-table>
    </el-tab-pane>
  </el-tabs>
</el-card>
<!-- 添加参数的对话框 -->
<el-dialog
  :title="'添加'+titleText"
  :visible.sync="addDialogVisible"
  width="50%" @close="addDialogClosed">
  <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
  <el-form-item :label="titleText" prop="attrName">
    <el-input v-model="addForm.attrName"></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="addDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addParams">确 定</el-button>
  </span>
</el-dialog>
<!-- 修改参数的对话框 -->
<el-dialog
  :title="'修改'+titleText"
  :visible.sync="editDialogVisible"
  width="50%" @close="editDialogClosed">
  <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
  <el-form-item :label="titleText" prop="attrName">
    <el-input v-model="editForm.attrName"></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editParams">确 定</el-button>
  </span>
</el-dialog>

    </div>
  
</template>

<script>
export default {
data(){
    return{
        catelist:[],
        //级联选择框的配置对象
        cateProps:{
            value:'catId',
            label:'catName',
            children:'children'
        },
        selectedCateKeys:[],
        activeName:"many",
        addDialogVisible:false,
        addForm:{
            attrName:''
        },
        editForm:{
            attrName:'',
            attrId:''
        },
        addFormRules:{
            attrName:[{required:true,message:'请输入参数名称',trigger:'blur'}]
        },
        editFormRules:{
            attrName:[{required:true,message:'请输入参数名称',trigger:'blur'}]
        },
        manyTableData:[],
        onlyTableData:[],
        editDialogVisible:false,
        b:false
        

    }
},
created(){
    this.getCateList()
},
methods:{
    //获取所以分类列表
  async getCateList(){
        const res= await this.$api.rear_end.categories()
      if(res.data.code===1){
        this.catelist=res.data.data
      }else{
        this.$message.error(res.data.msg)
      }
    },
    //级联选择框选中项变化，会触发这个函数
    handleChange(){
      if(this.selectedCateKeys.length!==2){
        this.selectedCateKeys=[]
        this.manyTableData=[]
        this.onlyTableData=[]
        return
      }
      this.getParamsData()
    },
    //获取参数的列表数据
   async getParamsData(){
    if(this.cateId!=null){
       const res=await this.$api.rear_end.attributes({sel:this.activeName},this.cateId)
    if(res.data.code===1){
        res.data.data.forEach(item => {   
                item.attrVals=item.attrVals?item.attrVals.split(' '):[] 
               
       
      
            });
        console.log(res.data.data)
if(this.activeName==='many'){
    this.manyTableData=res.data.data
}else{
    this.onlyTableData=res.data.data
}
    }else{
this.$message.error(res.data.msg)
    }}
    },
    //tab页签点击事件
    handleTabClick(){
        this.getParamsData()
    },
    //监听添加对话框的关闭事件
    addDialogClosed(){
    this.$refs.addFormRef.resetFields()
    },
    //监听修改对话框的关闭事件
    editDialogClosed(){
        this.$refs.editFormRef.resetFields()
    },

    //点击按钮，添加参数
    addParams(){
this.$refs.addFormRef.validate(async valid=>{
    if(!valid)return
    const add={
        attrName:this.addForm.attrName,
        catId:this.cateId,
        attrSel:this.activeName
    }
 const res=await this.$api.rear_end.addattr(add)
if(res.data.code===1){
    this.$message.success(res.data.data)
    this.addDialogVisible=false
    this.getParamsData()
}else{
    this.$message.error(res.data.msg)
}
})
    },
    //点击修改对话框
    showEditDialog(row){
    this.editForm.attrName=row.attrName
    this.editForm.attrId=row.attrId 
    this.editDialogVisible=true
    },
     //点击按钮，修改参数
    editParams(){
        this.$refs.editFormRef.validate(async valid=>{
    if(!valid)return
    const edit={
        attrName:this.editForm.attrName,
        catId:this.cateId,
        attrSel:this.activeName,
        attrId:this.editForm.attrId
    }
 const res=await this.$api.rear_end.editattr(edit)
if(res.data.code===1){
    this.$message.success(res.data.data)
    this.editDialogVisible=false
    this.getParamsData()

}else{
    this.$message.error(res.data.msg)
}
})
    },
    //点击删除按钮
    delParams(row){
        this.$confirm(`是否删除${this.titleText}？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        const delattr={
            attrId:row.attrId
        }
        const res = await this.$api.rear_end.delattr(delattr)
        if(res.data.code===1){
          this.$message.success(res.data.data) 
          this.getParamsData()
        }
        else{
          this.$message.error(res.data.msg)  
          
        }
      }).catch(() => {
        this.$message.error('取消删除')
      })
    },
    //将对attrVals的操作保存到数据库
    async saveAttrVals(row){
        const res=await this.$api.rear_end.editattr({
            attrId:row.attrId,
            attrName:row.attrName,
            attrSel:this.activeName,
            cateId:this.cateId,
            attrVals:row.attrVals.join(' '),
        })
        if(res.data.code===1){
            this.$message.success(res.data.data)
        }else{
            this.$message.error(res.data.msg)
            if(res.data.msg==='修改分类参数失败'){
                 row.attrVals.slice(0,row.attrVals.length-1)
            }
        }
    },
    
    //删除对应的参数可选项
    handleClose(i,row){
     row.attrVals.splice(i,1)
     this.saveAttrVals(row)
    },
   
    
},
computed:{
    isBtnDisabled(){
if(this.selectedCateKeys.length!==2)
{return true}
else{return false}
    },
    cateId(){
        if(this.selectedCateKeys.length===2){
            return this.selectedCateKeys[1]
        }else{
            return null
        }
    },
    titleText(){
        if(this.activeName==='many'){
            return '动态参数'
        }else{
            return '静态属性'
        }
    },
}
}
</script>

<style lang="less" scoped>

.cat_opt{
 margin: 15px 0;   
}
.el-tag{
    margin: 20px;
}


.input-new-tag{width: 120px;}
</style>